'use strict';

const Controller = require('egg').Controller;

/**
* @controller UplinkController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/

class UplinkController extends Controller {
  async list() {
       /**
        * @summary uplinklist。
        * @description uplinklist。
        * @router get /uplinklist
        * @request query integer limit
        * @request query integer page
        * @request query String OltId
        */
    const params = this.ctx.query
    const result = await this.ctx.service.uplink.list(params)
    this.ctx.body = {
        msg:"",
        status:200,
        data:result.rows,
        count:result.count
    }
  }
  async configRefresh() {
    const params = this.ctx.query
    const result = await this.ctx.service.uplink.configRefresh(params)
    let uplinkResult = await this.ctx.model.Uplink.findOne({where:{id:params.id}})
    let oltResult = await this.ctx.model.Olt.findOne({where:{id:uplinkResult.oltId}})
    global.log.desc = `${oltResult.name} ${oltResult.oltIP} ${uplinkResult.PortID} config refresh`
    this.ctx.body = {
      msg:result.msg,
      data:result.data
    }
  }
  async setUplinkMsg() {
    const params = this.ctx.request.body
    let result = await this.ctx.service.uplink.setUplinkMsg(params)
    let uplinkResult = await this.ctx.model.UplinkPorts.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:uplinkResult.oltId})
    global.log.desc = `${oltResult.name} ${oltResult.oltIP} ${uplinkResult.port} config edit`
    global.log.after = `addvlan:${params.tagVlan == "" ?null:params.tagVlan},Adminstatus:${params.type == 'up'?'Enable':'Disable'},UnTagvlan:${params.unTagVlan == ''? null:params.unTagVlan},removevlan:${params.removeTagVlan == ''? null:params.removeTagVlan}`
    this.ctx.body = {
      data:result,
      status:200
    }
  }

  // async uplinkCreateVlan(){
  //   const params = this.ctx.request.body
  //   let oltResult = await this.ctx.model.Olt.findOne({where:{id:params.oltId}})
  //   await this.ctx.service.uplink.uplinkCreateVlan(params)
  //   global.log.desc = `${oltResult.name} ${oltResult.oltIP} create vlan`
  //   global.log.after = `addvlan:${params.arr[0]}`
  //   this.ctx.body = {
  //     msg:""
  //   }
  // }

  //获取tagvlan 和 untagvlan
  async getUplinkMsg(){
    const params = this.ctx.query
    let result = await this.ctx.service.uplink.getUplinkMsg(params)
    this.ctx.body = {
      states:200,
      data:result
    }
  }
}

module.exports = UplinkController;
